package HDAPI.test;

import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

import HDAPI.HDBase;
import HDAPI.Settings;


/*
** Copyright (c) 2009
 * 
 * Richard Uren <richard@teleport.com.au>
 * Faisal Thaheem <faisal@computedsynergy.com>
 * 
** All Rights Reserved
**
** --
**
** LICENSE: Redistribution and use in source and binary forms, with or
** without modification, are permitted provided that the following
** conditions are met: Redistributions of source code must retain the
** above copyright notice, this list of conditions and the following
** disclaimer. Redistributions in binary form must reproduce the above
** copyright notice, this list of conditions and the following disclaimer
** in the documentation and/or other materials provided with the
** distribution.
**
** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
** NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
** OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
** TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
** DAMAGE.
**
** --
**
** This is a reference implementation for interfacing with www.handsetdetection.com
**
*/

public class test {
    
    public static void main(String args[]) throws Exception{
    	if (!Settings.init()) {
    		System.out.println("fail to load properties");
    	}
    	//testModel();
    	//testVendor();
    	//testDetect();
    	//mobile
    	String strUA = "Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95/12.0.013; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413";
    	//testDetectAll(strUA);
    	//tablet
    	strUA = "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
    	testDetectAll(strUA);
    	//console
    	strUA = "Mozilla/5.0 (PLAYSTATION 3; 2.00)";
    	testDetectAll(strUA);
    	testInformation();
    	//test download takes a long time, 
    	//uncomment below line to test download method
    	//testDownload();
    	//testMultipleIP();
    } 
    
    private static void testModel() 
    {
    	HDBase hd = new HDBase();
    	try{
            if(hd.model("Nokia")){
                System.out.println(hd.getModel().get(("message")));
                System.out.println(hd.getModel().toString());
            }else{
                System.out.println("failed");
                System.out.println(hd.getModel().get(("message")));
            }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
    
    private static void testVendor() 
    {
    	HDBase hd = new HDBase();
    	try{
            if(hd.vendor()) {
                System.out.println(hd.getVendor().get(("message")));
                System.out.println(hd.getVendor().toString());
            }else{
                System.out.println("failed");
                System.out.println(hd.getVendor().get(("message")));
            }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
    
    private static void testDetect() 
    {
    	HDBase hd = new HDBase();
    	hd.setDetectVar("user-agent", "Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95/12.0.013; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413");
    	try {
    		if (hd.detect("geoip, product_info, display")) {
	    		 System.out.println(hd.getDetect().get(("message")));
	             System.out.println(hd.getDetect().toString());
	    	}else{
	             System.out.println("failed");
	             System.out.println(hd.getDetect().get(("message")));
	    	}
    	} catch (Exception ex) {
    		 System.out.println(ex.getMessage());
    	}
    }

    private static void testDetectAll(String strUA) 
    {
    	HDBase hd = new HDBase();
    	System.out.println("Testing by user-agent: "  + strUA );
    	hd.setDetectVar("user-agent", strUA);
    	try {
    		if (hd.detectAll()) {
	    		 System.out.println(hd.getDetect().get(("message")));
	             System.out.println(hd.getDetect().toString());
	             System.out.println("test isMobile: " + hd.isMobile());
	             System.out.println("test isTablet: " + hd.isTablet());
	             System.out.println("test isConsole: " + hd.isConsole());
	    	}else{
	             System.out.println("failed");
	             System.out.println(hd.getDetect().get(("message")));
	    	}
    		
    	} catch (Exception ex) {
    		 System.out.println(ex.getMessage());
    	}
    }
    
    
    
    private static void testInformation() 
    {
    	HDBase hd = new HDBase();
    	try {
    		if (hd.information("Nokia", "N95")) {
	    		 System.out.println(hd.getInformation().get(("message")));
	             System.out.println(hd.getInformation().toString());
	    	}else{
	             System.out.println("failed");
	             System.out.println(hd.getInformation().get(("message")));
	    	}
    	} catch (Exception ex) {
    		 System.out.println(ex.getMessage());
    	}
    }
    
    private static void testDownload() 
    {
    	HDBase hd = new HDBase();
    	try {
    		if (hd.download()) {
    			System.out.println("File is successfully downloaded : " + hd.getDownloadFile().getAbsolutePath());
    		} else {
    			System.out.println("File download failed.");
    			
    		}
    	} catch (Exception ex) {
    		 System.out.println(ex.getMessage());
    	}
    }
    
    
    
    public static void testMultipleIP()
    {
    	try {
    		URL url = new URL("http://www.handsetdetection.com");
			InetAddress[] all = InetAddress.getAllByName((new URL("http://www.handsetdetection.com")).getHost());
			for (InetAddress addr : all) {
				System.out.println(addr.getHostAddress());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
    
    
}
